<?php
/**
 * Created by PhpStorm.
 * User: This
 * Date: 2019/1/6
 * Time: 0:10
 */
namespace app\index\controller;
use app\index\model\Product as p;
use Think\Db;

class Product extends Base{
    /**
     * @throws \think\exception\DbException
     */
    public function index(){
        $pages = $this->getVal('show_num');
        if($pages / 2 != 0){
            $pages *= 2;
        }
        $cate_id = 0;
        $pro = new p();
        if($_GET){
            if(isset($_GET['cate_id'])){
                $cate_id = $_GET['cate_id'];
                $cate_info = $pro->getCate(array('cate_pid'=>$cate_id),'cate_id');
                $str = $cate_id;
                if($cate_info){
                    foreach ($cate_info as $v){
                        $str .= ','.$v['cate_id'];
                    }
                }
                $condition['product_cate_id'] = array('in',$str);
            }
        }
        $condition['product_status'] = 1;
        $pro_info = $pro->getPage($condition,'product_id,product_name,product_img,product_remark,product_cate_id',$pages);
        $pro_cate = $pro->getCate(array('cate_status'=>1,'cate_pid'=>0),'cate_id,cate_name');
        $page = $pro_info->render();
        $location = '百步园艺';
        $img = '/baibu/public/static/image/index/t1.png';
        $desc = '花卉展示';
        $mark = 'product';
        $this->assign('cate_id',$cate_id);
        $this->assign('cate',$pro_cate);
        $this->assign('product',$pro_info);
        $this->assign('mark',$mark);
        $this->assign('img',$img);
        $this->assign('location',$location);
        $this->assign('desc',$desc);
        $this->assign('page',$page);
        $this->assign('menu',json_encode($this->m));
        return $this->fetch('index');
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail(){
        if(!$_GET){
            ajaxReturn(0,'参数错误！',0);
        }
        $location = '百步园艺';
        $img = '/baibu/public/static/image/index/t1.png';
        $desc = '花卉展示';
        $mark = 'product';
        $pro = new p();
        $condition['product_id'] = $_GET['product_id'];
        $pro_cate = $pro->getCate(array('cate_status'=>1,'cate_pid'=>0),'cate_id,cate_name');
        $pro_info = $pro->getOne($condition,'product_id,product_name,product_img,product_remark,product_desc,product_cate_id');
        $cate = Db::name('product_category')->field('cate_pid')->where(array('cate_id'=>$pro_info['product_cate_id']))->find();
        if($cate['cate_pid'] != 0){
            $cate_id = $cate['cate_pid'];
        }else{
            $cate_id = $pro_info['product_cate_id'];
        }
        $this->assign('product',$pro_info);
        $this->assign('cate_id',$cate_id);
        $this->assign('cate',$pro_cate);
        $this->assign('mark',$mark);
        $this->assign('img',$img);
        $this->assign('location',$location);
        $this->assign('desc',$desc);
        $this->assign('menu',json_encode($this->m));
        return $this->fetch('show');
    }
}